﻿using Microsoft.Win32;
using RRQMSocket;
using RRQMSocket.FileTransfer;
using System;
using System.Diagnostics;
using System.IO;
using System.Timers;
using System.Windows;
using System.Windows.Controls;

namespace Views
{
    /// <summary>
    /// OtherTransferFilePanel.xaml 的交互逻辑
    /// </summary>
    public partial class OtherTransferFilePanel : UserControl
    {
        public OtherTransferFilePanel()
        {
            InitializeComponent();
        }

        public string FileName
        {
            get { return (string)GetValue(FileNameProperty); }
            set { SetValue(FileNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FileName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileNameProperty =
            DependencyProperty.Register("FileName", typeof(string), typeof(OtherTransferFilePanel), new PropertyMetadata("1.txt"));

        public string FileLength
        {
            get { return (string)GetValue(FileLengthProperty); }
            set { SetValue(FileLengthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FileLength.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileLengthProperty =
            DependencyProperty.Register("FileLength", typeof(string), typeof(OtherTransferFilePanel), new PropertyMetadata("10Mb"));

        public string FilePath
        {
            get { return (string)GetValue(FilePathProperty); }
            set { SetValue(FilePathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FilePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FilePathProperty =
            DependencyProperty.Register("FilePath", typeof(string), typeof(OtherTransferFilePanel), new PropertyMetadata("1.txt"));

        public double UploadProgressValue
        {
            get { return (double)GetValue(UploadProgressValueProperty); }
            set { SetValue(UploadProgressValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for UploadProgressValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UploadProgressValueProperty =
            DependencyProperty.Register("UploadProgressValue", typeof(double), typeof(OtherTransferFilePanel), new PropertyMetadata(0.0));

        public string SendSpeed
        {
            get { return (string)GetValue(SendSpeedProperty); }
            set { SetValue(SendSpeedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SendSpeed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SendSpeedProperty =
            DependencyProperty.Register("SendSpeed", typeof(string), typeof(OtherTransferFilePanel), new PropertyMetadata("100kb/s"));

        public Visibility ReceiveSuccess
        {
            get { return (Visibility)GetValue(ReceiveSuccessProperty); }
            set { SetValue(ReceiveSuccessProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ReceiveSuccess.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ReceiveSuccessProperty =
            DependencyProperty.Register("ReceiveSuccess", typeof(Visibility), typeof(OtherTransferFilePanel), new PropertyMetadata(Visibility.Hidden));

        public Visibility BeginReceive
        {
            get { return (Visibility)GetValue(BeginReceiveProperty); }
            set { SetValue(BeginReceiveProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BeginReceive.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BeginReceiveProperty =
            DependencyProperty.Register("BeginReceive", typeof(Visibility), typeof(OtherTransferFilePanel), new PropertyMetadata(Visibility.Visible));

        private bool isReceived;

        public bool IsReceived
        {
            get { return isReceived; }
            set
            {
                isReceived = value;
                if (value)
                {
                    this.ReceiveSuccess = Visibility.Visible;
                    this.BeginReceive = Visibility.Hidden;
                }
                else
                {
                    this.ReceiveSuccess = Visibility.Hidden;
                    this.BeginReceive = Visibility.Visible;
                }
            }
        }

        private Timer timer;
        private FileClient fileClient;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            fileClient = new FileClient();

            fileClient.ReceiveDirectory = AppDomain.CurrentDomain.BaseDirectory;

            ConnectSetting connectSetting = new ConnectSetting();

            fileClient.DownloadFileFinshed += FileClient_DownloadFileFinshed; ;

            try
            {
                fileClient.Connect(connectSetting);
                FileUrl url = new FileUrl();
                url.FilePath = this.FilePath;

                try
                {
                    fileClient.DownloadFile(url);
                    timer = new Timer(1000);
                    timer.Elapsed += Timer_Elapsed; ;
                    timer.Start();
                }
                catch (Exception)
                {
                }
            }
            catch (Exception)
            {
            }
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                this.UploadProgressValue = fileClient.DownloadProgressValue;
                this.SendSpeed = string.Format("{0}Kb/s", fileClient.DownloadSpeed.ToString("0.00"));
            });
        }

        private void FileClient_DownloadFileFinshed(object sender, FileFinishedArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                timer.Stop();
                timer.Dispose();

                this.UploadProgressValue = 1;
                this.SendSpeed = string.Empty;
                //this.Text = "接收完成";
                this.IsReceived = true;
            });
        }

        private void Button_OpenFile(object sender, RoutedEventArgs e)
        {
            Process.Start(this.FilePath);
        }

        private void Button_SaveOther(object sender, RoutedEventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();

            fileDialog.FileName = this.FileName;
            fileDialog.ShowDialog();
            string s = fileDialog.FileName;
            if (s != null || s != string.Empty)
            {
                File.Copy(this.FilePath, s);
                //MainLog.Log("保存成功");
            }
        }
    }
}